package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Department;
import unipi.database.ErrorCodes;
import unipi.database.Rights;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class EditDepartment  extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public EditDepartment()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String dptID, String message)
	{
		String script = "<script type=\"text/javascript\">" +
				"function loadData()" +
				"{" +
				"id = document.getElementById(\"ddldptid\").value;" +
				"window.location = \"EditDepartment?id=\" + id;" +
				"}" +
				"</script>";
		
		String content = "<h1>Επεξεργασία Τμήματος</h1>";
		
		List<List<String>> dpts = Rights.getDpts(uid, Rights.RightCols.DptRights);
		
		String dName = "";
		String dDesc = "";
		if(dptID != null)
		if(!dptID.equals("none"))
		{
			List<List<String>> dptData = Department.getDptInfo(dptID);
			
			if(dptData != null)
			{
				dName = dptData.get(0).get(0);
				dDesc = dptData.get(0).get(1);
			}		
		}
		
		content += HTMLPages.getSubmitForm("EditDepartment", 
						new String[]
								{
									"Επιλογή Τμήματος",
									"Διαγραφή;",
									"Όνομα",
									"Περιγραφή"
								},
						new String[]
								{
										"<div class=\"ddldivstyle\">" +
													HTMLPages.getDropDownList(dpts, "dptid", dptID == null?"none":dptID, "--Επιλέξτε Τμήμα--",
															"id=\"ddldptid\" onchange=\"loadData()\"") + "</div>",
										"<input type=\"checkbox\" name=\"delete\" val=\"del\" />",
										"<input class=\"mediumInput\" name=\"name\" type=\"text\" value=\"" + dName + "\" />",
										"<textarea  class=\"txtarea\" name=\"desc\" rows=\"6\" cols=\"40\" >" + dDesc + "</textarea>"				
								}
						, "Επεξεργασία") + message;
		
		
		return HTMLPages.getGeneralPage(content, script, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			
			String dptID = request.getParameter("dptid");
			boolean delete = request.getParameter("delete") == null? false: true;
			String newname = request.getParameter("name");
			String newdesc = request.getParameter("desc");
			
			String message;
			
			if(dptID.equals("none"))
				message = "Δεν εχετε επιλέξει τμήμα.";
			else
			{
				if(delete)
				{
					List<List<String>> users = Department.getUsers(dptID);
					if(users != null)
						if(users.size() == 0)
						{
							ErrorCodes del = Department.Delete(dptID);
							if(del != ErrorCodes.OK)
								message = "Παρουσιάστηκε σφάλμα κατα την διαγραφή του τμήματος.";
							else
								message = "Το τμήμα διαγράφθηκε με επιτυχία.";
						}
						else
							message = "Το τμήμα δεν μπορει να διαγραφεί ενώ περιέχει χρήστες.";
					else
						message = "Παρουσιάστηκε σφάλμα κατα την διαγραφή του τμήματος.";					
				}				
				else
				{
					if(newname != null && newdesc != null)
					{
						newname = Validator.escape(newname);
						newdesc = Validator.escape(newdesc);
						
						ErrorCodes ed = Department.Edit(dptID, newname, newdesc);
						if(ed != ErrorCodes.OK)
						{
							message = "Παρουσιάστηκε σφάλμα κατα την επεξεργασία του τμήματος.";								
						}
						else
							message = "Το τμήμα επεξεργάστηκε με επιτυχία";
					}
					else
						message = "Δεν εχετε εισάγει όλα τα δεδομένα.";
				}
				
			}
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, null, message));
			
			out.close();			
		}
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String dptID = request.getParameter("id");
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, dptID, ""));
			
			out.close();			
		}
	}
	
}
